#coding=utf-8
from rest_framework import serializers
from datetime import datetime


from .models import *
import requests
import datetime
import time
from django.db.models import *
class StoreReportSerializer(serializers.ModelSerializer):
	
    class Meta:
        model = store_report
	fields = "__all__"
class VodErrorLogSerializer(serializers.ModelSerializer):
	
    Info = serializers.SerializerMethodField()
    
    def get_Info(self,obj):
       return list(machine_relation.objects.filter(mid=obj.mid).values("merchantid","storeid"))
    class Meta:
        model = Vod_errorlog
	fields = "__all__"

class productOrderSerializer(serializers.ModelSerializer):

    
    productInfo = serializers.SerializerMethodField()
    
    def get_productInfo(self,obj):
       return list(product_info.objects.filter(productid=obj.productid).values())

    class Meta:
        model = product_order
	fields = "__all__"
class storeinfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = store_info 
	fields = "__all__"
class machineinfoSerializer(serializers.ModelSerializer):

    storeid = serializers.SerializerMethodField()
    merchantid = serializers.SerializerMethodField()

    def get_storeid(self,obj):
        try:
            result = machine_relation.objects.get(mid=obj.MachineId)
	except:
	    return 
	storeid =result.storeid
        return storeid
    def get_merchantid(self,obj):
        try:	
            result = machine_relation.objects.get(mid=obj.MachineId)
	except:
	    return 
	merchantid = result.merchantid
        return merchantid
    class Meta:
        model = MachineInfo 
	fields = "__all__"

class merchantinfoSerializer(serializers.ModelSerializer):
    NumberOfMachine = serializers.SerializerMethodField()
    all_income1 = serializers.SerializerMethodField()

    def get_all_income1(self,obj):
        result = list(machine_relation.objects.filter(merchantid=obj.merchantid).values("mid"))
	mid_list=[]
	for row in result:
	    mid_list.append(row["mid"])

	count = machine_report.objects.filter(mid__in=mid_list).aggregate(Sum('all_income'))["all_income__sum"]
	if not count :
	    count = 0

	return count



    def get_NumberOfMachine(self,obj):
        """
	商户下设备数量
	默认情况下注册一台机器未激活不算入
	"""
        #count = machine_relation.objects.filter(merchantid=obj.merchantid).count()
        result = machine_relation.objects.filter(merchantid=obj.merchantid).values("mid")

        mid_list = []
	for row in result :
	    mid_list.append(row["mid"])
	count = MachineInfo.objects.filter(MachineId__in=mid_list,STATUS=1).count()
        return count
    def create(self, validated_data):
	new_user=super(merchantinfoSerializer, self).create(validated_data=validated_data)
        user = self.context['request'].user 
        return new_user
    class Meta:
        model = merchant_info
  	fields = "__all__"

class merchantselfSerializer(serializers.ModelSerializer):
    #password = serializers.CharField(max_length=50,error_messages={"blank":"密码不能为空","required": "请填写密码"})

    #mobile = serializers.CharField(max_length=50,error_messages={"blank":"手机号不能为空","required": "请填写手机号"})
    class Meta:
        model = merchant_info
  	fields = "__all__"

class userconsumeSerializer(serializers.ModelSerializer):
    nickname =  serializers.SerializerMethodField()
    machinename = serializers.SerializerMethodField()
    picurl = serializers.SerializerMethodField()


    def get_machinename(self,obj):
        result = MachineInfo.objects.get(MachineId=obj.mid)
        return result.MACHINE_NAME
    def get_nickname(self,obj):
        result = UserInfo.objects.get(UID=obj.uid)
        return result.NickName
    def get_picurl(self,obj):
        result = UserInfo.objects.get(UID=obj.uid)
        return result.HeadImgUrl
    class Meta:
        model = user_consume
  	fields = "__all__"
class userrechargeSerializer(serializers.ModelSerializer):
    nickname =  serializers.SerializerMethodField()
    machinename = serializers.SerializerMethodField()

    def get_machinename(self,obj):
        result = MachineInfo.objects.get(MachineId=obj.mid)
        return result.MACHINE_NAME
    def get_nickname(self,obj):
        result = UserInfo.objects.get(UID=obj.uid)
        return result.NickName
    class Meta:
        model = user_recharge
  	fields = "__all__"
class UserInfoSerializer(serializers.ModelSerializer):

    recharge_history = serializers.SerializerMethodField()
    consume_history = serializers.SerializerMethodField()

    def get_recharge_history(self,obj):
        datalist = []
        record = user_recharge.objects.filter(uid = obj.UID)
	for row in record:
	    _ = {"uid":row.uid,"mount":row.mount}
	    datalist.append(_)
	return datalist
    
    def get_consume_history(self,obj):
        datalist = []
        record = user_consume.objects.filter(uid = obj.UID)
	for row in record:
	    _ = {"uid":row.uid,"mount":row.mount}
	    datalist.append(_)
	return datalist


    class Meta:
        model = UserInfo
	fields = "__all__"
    
 
class MachineActiveStatusSerializer(serializers.ModelSerializer):
    ActiveUrl = serializers.SerializerMethodField()
    def get_ActiveUrl(self,obj):
        return {"musicpro_mid":int(obj.MachineId)}

    class Meta:
        model = MachineInfo
	fields = ["MachineId","STATUS","ActiveUrl"]
